package org.example.test5.service.serviceImpl;

import org.example.test5.mapper.CardMapper;
import org.example.test5.mapper.UserMapper;
import org.example.test5.service.CardService;
import org.example.test5.model.Card;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class CardServiceImpl implements CardService {

    @Autowired
    private CardMapper cardMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Card findCardById(int id) {
        return cardMapper.findCardById(id);
    }

    @Transactional
    @Override
    public void addCard(Card card) {
        cardMapper.insertCard(card);
    }

    @Transactional
    @Override
    public void updateCard(Card card) {
        cardMapper.updateCard(card);
    }

    @Transactional
    @Override
    public void deleteCard(int id) {
        cardMapper.deleteCard(id);
    }

    @Override
    public List<Card> findAll() {
        return cardMapper.findAll();
    }

    @Override
    public void updatePassword(String name, String newPassword) {
        System.out.println(name+" "+newPassword);
        int userId = cardMapper.getUserIdByName(name);
        userMapper.updatePassword(userId,newPassword);
    }
}